<template>
  <div class="demo-collapse">
    <el-collapse v-model="activeNames" @change="handleChange">
      <el-collapse-item name="1">
        <template #title>
            <el-tag type="primary" style="margin-left: 30px;">添加线路</el-tag>
        </template>
        <el-form ref="roadlinksRef" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="所属路书" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入所属路书" />
        </el-form-item> -->
            <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入名称" />
            </el-form-item>
            <el-form-item label="笔记" prop="note">
            <el-input v-model="form.note" placeholder="请输入笔记" />
            </el-form-item>
            <el-form-item label="交通方式" prop="method">
            <el-select v-model="form.method" placeholder="请选择交通方式">
                <el-option
                v-for="dict in map_vehicles_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
                ></el-option>
            </el-select>
            </el-form-item>
            <el-form-item label="道路类型" prop="roadtype">
            <el-select v-model="form.roadtype" placeholder="请选择道路类型">
                <el-option
                v-for="dict in map_road_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
                ></el-option>
            </el-select>
            </el-form-item>
            <el-form-item label="预计时间" prop="predicteTime">
            <el-input v-model="form.predicteTime" placeholder="请输入预计时间" />
            </el-form-item>
            <el-form-item label="缩略图" prop="thumbnail">
            <image-upload v-model="form.thumbnail"/>
            </el-form-item>
            <el-form-item label="其他" prop="other">
            <el-input v-model="form.other" placeholder="请输入其他" />
            </el-form-item>
            <!-- <el-divider content-position="center">节点信息</el-divider>
            <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" icon="Plus" @click="handleAddMapNodes">添加</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" icon="Delete" @click="handleDeleteMapNodes">删除</el-button>
            </el-col>
            </el-row>
            <el-table :data="mapNodesList" :row-class-name="rowMapNodesIndex" @selection-change="handleMapNodesSelectionChange" ref="mapNodes">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="序号" align="center" prop="index" width="50"/>
            <el-table-column label="线路名称" prop="linkName" width="150">
                <template #default="scope">
                <el-input v-model="scope.row.linkName" placeholder="请输入线路名称" />
                </template>
            </el-table-column>
            <el-table-column label="节点名称" prop="nodeName" width="150">
                <template #default="scope">
                <el-input v-model="scope.row.nodeName" placeholder="请输入节点名称" />
                </template>
            </el-table-column>
            <el-table-column label="是否线路" prop="isPath" width="150">
                <template #default="scope">
                <el-select v-model="scope.row.isPath" placeholder="请选择是否线路">
                    <el-option
                    v-for="dict in sys_yes_no"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                    ></el-option>
                </el-select>
                </template>
            </el-table-column>
            <el-table-column label="序号" prop="serial" width="150">
                <template #default="scope">
                <el-input v-model="scope.row.serial" placeholder="请输入序号" />
                </template>
            </el-table-column>
            <el-table-column label="笔记" prop="note" width="150">
                <template #default="scope">
                <el-input v-model="scope.row.note" placeholder="请输入笔记" />
                </template>
            </el-table-column>
            <el-table-column label="坐标" prop="position" width="150">
                <template #default="scope">
                <el-input v-model="scope.row.position" placeholder="请输入坐标" />
                </template>
            </el-table-column>
            <el-table-column label="类型" prop="type" width="150">
                <template #default="scope">
                <el-select v-model="scope.row.type" placeholder="请选择类型">
                    <el-option
                    v-for="dict in map_node_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                    ></el-option>
                </el-select>
                </template>
            </el-table-column>
            <el-table-column label="备注" prop="remark" width="150">
                <template #default="scope">
                <el-input v-model="scope.row.remark" placeholder="请输入备注" />
                </template>
            </el-table-column>
            <el-table-column label="其他" prop="other" width="150">
                <template #default="scope">
                <el-input v-model="scope.row.other" placeholder="请输入其他" />
                </template>
            </el-table-column>
            </el-table> -->
        </el-form>
        <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
        </div>
      </el-collapse-item>
      <el-collapse-item name="2">
        <template #title>
            <el-tag type="primary" style="margin-left: 30px;">线路列表</el-tag>
        </template>
        <div class="app-container">
            <el-table v-loading="loading" :data="roadlinksList" >
              <!-- @selection-change="handleSelectionChange" -->
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column label="名称" align="center" prop="name" />
            <!-- <el-table-column label="其他" align="center" prop="other" /> -->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['work:roadlinks:edit']">修改</el-button> -->
                 <el-button link type="primary" icon="Position" @click="handleSelect(scope.row)">选择</el-button>
                <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['work:roadlinks:remove']">删除</el-button>
                </template>
            </el-table-column>
            </el-table>
            
            <!-- <pagination
            v-show="total>0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
            /> -->
            <!-- 用户导入对话框 -->
            <el-dialog :title="upload.title" v-model="importopen" width="400px">
            <el-upload
            ref="uploadRef"
            :limit="1"
            accept=".xlsx, .xls"
            :headers="upload.headers"
            :action="upload.url + '?updateSupport=' + upload.updateSupport"
            :disabled="upload.isUploading"
            :on-progress="handleFileUploadProgress"
            :on-success="handleFileSuccess"
            :auto-upload="false"
            drag
            >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">
                将文件拖到此处，或
                <em>点击上传</em>
            </div>
            <div class="el-upload__tip" slot="tip">
                <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据
                <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
            </div>
            <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
            </el-upload>
            <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitFileForm">确 定</el-button>
            <el-button @click="upload.open = false">取 消</el-button>
            </div>
            </el-dialog>

            <!-- 添加或修改路线对话框 -->
            <el-dialog :title="title" v-model="open" width="900px" append-to-body>
            <el-form ref="roadlinksRef" :model="form" :rules="rules" label-width="80px">
              <el-row :gutter="20">
                <el-col :span="12">
                <el-divider content-position="center">路线信息</el-divider>
                <el-form-item label="所属路书" prop="parentId">
                <el-input v-model="form.parentId" placeholder="请输入所属路书" />
                </el-form-item>
                <el-form-item label="名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入名称" />
                </el-form-item>
                <el-form-item label="笔记" prop="note">
                <el-input v-model="form.note" placeholder="请输入笔记" />
                </el-form-item>
                <el-form-item label="交通方式" prop="method">
                <el-select v-model="form.method" placeholder="请选择交通方式">
                    <el-option
                    v-for="dict in map_vehicles_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                    ></el-option>
                </el-select>
                </el-form-item>
                <el-form-item label="道路类型" prop="roadtype">
                <el-select v-model="form.roadtype" placeholder="请选择道路类型">
                    <el-option
                    v-for="dict in map_road_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                    ></el-option>
                </el-select>
                </el-form-item>
                <el-form-item label="预计时间" prop="predicteTime">
                <el-input v-model="form.predicteTime" placeholder="请输入预计时间" />
                </el-form-item>
                <el-form-item label="缩略图" prop="thumbnail">
                <image-upload v-model="form.thumbnail"/>
                </el-form-item>
                <el-form-item label="其他" prop="other">
                <el-input v-model="form.other" placeholder="请输入其他" />
                </el-form-item>
                
                </el-col>
                <el-col :span="12">
                <el-divider content-position="center">节点信息</el-divider>
                <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" icon="Plus" @click="handleAddMapNodes">添加</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="danger" icon="Delete" @click="handleDeleteMapNodes">删除</el-button>
                </el-col>
                </el-row>
                <el-table :data="mapNodesList" :row-class-name="rowMapNodesIndex" @selection-change="handleMapNodesSelectionChange" ref="mapNodes">
                <el-table-column type="selection" width="50" align="center" />
                <!-- <el-table-column label="序号" align="center" prop="index" width="50"/> -->
                <!-- <el-table-column label="线路名称" prop="linkName" width="150">
                    <template #default="scope">
                    <el-input v-model="scope.row.linkName" placeholder="请输入线路名称" />
                    </template>
                </el-table-column> -->
                <el-table-column label="节点名称" prop="nodeName" width="150">
                    <template #default="scope">
                    <el-input v-model="scope.row.nodeName" placeholder="请输入节点名称" />
                    </template>
                </el-table-column>
                <el-table-column label="是否线路" prop="isPath" width="150">
                    <template #default="scope">
                    <el-select v-model="scope.row.isPath" placeholder="请选择是否线路">
                        <el-option
                        v-for="dict in sys_yes_no"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                        ></el-option>
                    </el-select>
                    </template>
                </el-table-column>
                <el-table-column label="序号" prop="serial" width="150">
                    <template #default="scope">
                    <el-input v-model="scope.row.serial" placeholder="请输入序号" />
                    </template>
                </el-table-column>
                <el-table-column label="笔记" prop="note" width="150">
                    <template #default="scope">
                    <el-input v-model="scope.row.note" placeholder="请输入笔记" />
                    </template>
                </el-table-column>
                <el-table-column label="坐标" prop="position" width="150">
                    <template #default="scope">
                    <el-input v-model="scope.row.position" placeholder="请输入坐标" />
                    </template>
                </el-table-column>
                <el-table-column label="类型" prop="type" width="150">
                    <template #default="scope">
                    <el-select v-model="scope.row.type" placeholder="请选择类型">
                        <el-option
                        v-for="dict in map_node_type"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                        ></el-option>
                    </el-select>
                    </template>
                </el-table-column>
                <el-table-column label="备注" prop="remark" width="150">
                    <template #default="scope">
                    <el-input v-model="scope.row.remark" placeholder="请输入备注" />
                    </template>
                </el-table-column>
                <el-table-column label="其他" prop="other" width="150">
                    <template #default="scope">
                    <el-input v-model="scope.row.other" placeholder="请输入其他" />
                    </template>
                </el-table-column>
                </el-table>
                </el-col>
              </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
            </el-dialog>
        </div>
      </el-collapse-item>
    </el-collapse>
  </div>
</template>

<script setup name="Roadlinks">
import { listRoadlinks, getRoadlinks, delRoadlinks, addRoadlinks, updateRoadlinks } from "@/api/work/roadlinks"
import { getToken } from '@/utils/auth'
const { proxy } = getCurrentInstance()
const { sys_yes_no, map_road_type, map_vehicles_type, map_node_type } = proxy.useDict('sys_yes_no', 'map_road_type', 'map_vehicles_type', 'map_node_type')

const props = defineProps(['roadbookId','roadbookName'])

const emit = defineEmits(['addLink','selectLink'])

const roadlinksList = ref([])
const mapNodesList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const checkedMapNodes = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    parentId: null,
    parentName: null,
    name: null,
    note: null,
    method: null,
    roadtype: null,
    predicteTime: null,
    thumbnail: null,
    other: null
  },
  rules: {
    parentId: [
      { required: true, message: "所属路书不能为空", trigger: "blur" },
    ],
    name:[
      { required: true, message: "名称不能为空", trigger: "blur" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

const activeNames = ref(['0'])
function handleChange(val){
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    parentId: props.roadbookId,
    parentName: props.roadbookName,
    name: null,
    note: null,
    method: null,
    roadtype: null,
    predicteTime: null,
    thumbnail: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    other: null
  }
  mapNodesList.value = []
  proxy.resetForm("roadlinksRef")
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 查询路线列表 */
function getList() {
  loading.value = true
 
  listRoadlinks(queryParams.value).then(response => {
    roadlinksList.value = response.rows
    console.log(roadlinksList)
    total.value = response.total
    loading.value = false
  })
}


/** 提交按钮 */
function submitForm() {
  proxy.$refs["roadlinksRef"].validate(valid => {
    if (valid) {
      form.value.mapNodesList = mapNodesList.value
      if (form.value.id != null) {
        updateRoadlinks(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addRoadlinks(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          activeNames.value = ['0']
          getList()
          emit('addLink', {
            code:response.code
          })
        })
      }
    }
  })
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getRoadlinks(_id).then(response => {
    form.value = response.data
    mapNodesList.value = response.data.mapNodesList
    open.value = true
    title.value = "修改路线"
  })
}
/** 选择 */
function handleSelect(row){
  console.log(row)
  emit('selectLink',row)
}
/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除路线编号为"' + _ids + '"的数据项？').then(function() {
    return delRoadlinks(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}
/** 节点序号 */
function rowMapNodesIndex({ row, rowIndex }) {
  row.index = rowIndex + 1
}

/** 节点添加按钮操作 */
function handleAddMapNodes() {
  let obj = {}
  obj.linkName = ""
  obj.nodeName = ""
  obj.isPath = ""
  obj.serial = ""
  obj.note = ""
  obj.position = ""
  obj.type = ""
  obj.remark = ""
  obj.other = ""
  obj.thumbnail = ""
  mapNodesList.value.push(obj)
}

/** 节点删除按钮操作 */
function handleDeleteMapNodes() {
  if (checkedMapNodes.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的节点数据")
  } else {
    const mapNodess = mapNodesList.value
    const checkedMapNodess = checkedMapNodes.value
    mapNodesList.value = mapNodess.filter(function(item) {
      return checkedMapNodess.indexOf(item.index) == -1
    })
  }
}

/** 复选框选中数据 */
function handleMapNodesSelectionChange(selection) {
  checkedMapNodes.value = selection.map(item => item.index)
}


//导入对话框开启标记
const importopen = ref(false);
const uploadRef = ref()
const upload = reactive({
  // 设置上传的请求头部
  headers: {Authorization: "Bearer " + getToken()},
  updateSupport:0,
  // 上传的地址
  url: import.meta.env.VITE_APP_BASE_API + "work/roadlinks/importData",
})


// 文件上传中处理
function handleFileUploadProgress(event, file, fileList) {
  upload.isUploading = true;
}
// 文件上传成功处理
function handleFileSuccess(response, file, fileList) {
  upload.open = false;
  upload.isUploading = false;
  uploadRef.value.clearFiles();
  //this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
  // console.log(response.msg, "导入结果", { dangerouslyUseHTMLString: true })
  getList();
}
// 提交上传文件
function submitFileForm() {
  uploadRef.value.submit();
}

function init(){
  form.value.parentId = props.roadbookId
  form.value.parentName = props.roadbookName
  getList()
}
init()
</script>


